package com.caigou.controller;

import com.caigou.annotation.OperLog;
import com.caigou.bean.cms.entity.SurveyForm;
import com.caigou.bean.cms.entity.SurveyFormConfig;
import com.caigou.bean.cms.entity.SurveyQuestion;
import com.caigou.bean.cms.entity.SurveySet;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.exam.entity.Users;
import com.caigou.bean.param.ContentsParam;
import com.caigou.bean.param.SubmitSurveyFromParam;
import com.caigou.bean.param.SurveyPageParam;
import com.caigou.bean.vo.SurveyPageVO;
import com.caigou.bean.vo.SurveyQuestionVO;
import com.caigou.constant.NewsConstant;
import com.caigou.enums.FailResultEnum;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.ISurveyService;
import com.caigou.service.ISurveySetService;
import com.caigou.service.IUsersService;
import com.caigou.util.Result;
import com.caigou.util.StringUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@Slf4j
@RestController
@RequestMapping("/survey")
public class SurveyController {

    @Resource
    private ISurveyService surveyService;
    @Resource
    private IUsersService usersService;

    @Resource
    private ISurveySetService surveySetService;

    /**
     * 查询表单的接口
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/queryForm")
    public Result queryForm(@RequestBody QueryFormParam param, Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            PageInfo<SurveyForm> pageInfo = surveyService.queryForm(param);
            return Result.success(pageInfo);
        }
    }

    @PostMapping("/queryForm4Catalog")
    public Result queryForm4Catalog(@RequestBody QueryFormParam param, Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            PageInfo<SurveyForm> pageInfo = surveyService.queryForm4Catalog(param);
            return Result.success(pageInfo);
        }
    }

    /**
     * 根据formId获取智能表单详情
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/getFormDetail")
    public Result getFormDetail(@RequestBody SurveyFormParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else {
            if(param.getFormId()==null){
                return Result.error(ResponseEnum.PARAM_ERROR);
            }
            SurveyForm surveyForm = surveyService.getFormByFormId(param.getFormId());
            return Result.success(surveyForm);
        }
    }

    /**
     * 对表单“禁用”、“启用”
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/updateFormEnableStatus")
    @OperLog(operModule = NewsConstant.FORM_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "禁用表单")
    public Result updateFormEnableStatus(@RequestBody SurveyFormParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else {
            if(param.getFormId()==null){
                return Result.error(ResponseEnum.PARAM_ERROR);
            }
            return surveyService.updateIsEnableByFormId(param, user);
        }
    }

    /**
     * 删除到回收站
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/deleteForm")
    @OperLog(operModule = NewsConstant.FORM_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除表单")
    public Result deleteForm(@RequestBody SurveyFormParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else {
            if(param.getFormId()==null){
                return Result.error(ResponseEnum.PARAM_ERROR);
            }
            return surveyService.deleteFormByFormId(param, user);
        }
    }

    /**
     * 通过智能表单类型获取“预创建表单”信息
     * @return
     */
    @PostMapping("/getPreFormByFormType")
    public Result getPreFormByFormType(@RequestBody SurveyFormParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            SurveyForm surveyForm = surveyService.getPreFormByFormType(param.getFormType(), user);
            return Result.success(surveyForm);
        }
    }

    /**
     * 获取表单头部公共信息
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/getFormHeader")
    public Result getFormHeader(@RequestBody SurveyFormParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            if(StringUtils.isEmpty(param.getFormId())){
                return Result.error("缺少formId参数", "缺少formId参数");
            }
            SurveyFormHeader header = surveyService.getFormHeader(param.getFormId(), param.getFormType(), user);
            return Result.success(header);
        }
    }

    @PostMapping("/removeFormInCatalog")
    public Result removeFormInCatalog(@RequestBody FormCatalogParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            if(StringUtils.isEmpty(param.getFormId())||StringUtils.isEmpty(param.getCatalogId())){
                return Result.error("缺少catalogId或者formId参数", "缺少catalogId或者formId参数");
            }
            return surveyService.removeFormInCatalog(param.getCatalogId(), param.getFormId());
        }
    }


    /**
     * 保存表单接口，支持创建/修改
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/saveForm")
    @OperLog(operModule = NewsConstant.FORM_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "创建表单")
    public Result saveForm(@RequestBody SurveyFormParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            if(StringUtils.isEmpty(param.getFormId())){
                return Result.error("表单formId不能为空","表单formId不能为空");
            }
            int result = surveyService.createOrUpdateSurveyForm(param, user);
            if(result>0){
                return Result.success("创建/修改智能表单成功");
            }else{
                return Result.error("创建/修改智能表单失败", "创建/修改智能表单失败");
            }
        }
    }

    /**
     * 题目及选项接口（支持创建或者修改）
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/saveQuestionAndOption")
    public Result saveQuestionAndOption(@RequestBody QuestionAndOptionParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            String formId = param.getQuestionParam().getFormId();
            if(StringUtils.isEmpty(formId)){
                return Result.error("缺少formId参数", "缺少formId参数");
            }
            String placeholder = param.getQuestionParam().getPlaceholder();
            if(placeholder!=null){
                if(placeholder.length()>50){
                    return Result.error("提示不能超过50个字符", "提示不能超过50个字符");
                }
            }
            param.getQuestionParam().getLabel();
            //首先判断surveyForm是否已经创建
            SurveyForm surveyForm = surveyService.getFormByFormId(formId);
            if(surveyForm==null){
                return Result.custom(555, "请先保存智能表单", "请先保存智能表单");
            }
            return surveyService.createOrUpdateQuestionAndOption(param);
        }
    }

    /**
     * 创建题目的单个选项
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/createOption")
    public Result createOption(@RequestBody Option param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            return surveyService.createOption(param);
        }
    }


    /**
     * 更新题目的单个选项
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/updateOption")
    public Result updateOption(@RequestBody Option param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            return surveyService.updateOption(param);
        }
    }

    /**
     * 删除题目及选项
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/deleteQuestion")
    public Result deleteQuestion(@RequestBody QuestionParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            int result = surveyService.deleteQuestionAndOption(param);
            if(result>0){
                return Result.success("删除题目及选项成功");
            }else{
                return Result.error("删除题目及选项失败", "删除题目及选项失败");
            }
        }
    }

    /**
     * 删除选项接口
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/deleteOption")
    public Result deleteOption(@RequestBody Option param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            int result = surveyService.deleteOption(param);
            if(result>0){
                return Result.success("删除选项成功");
            }else{
                return Result.error("删除选项成功失败", "删除选项成功失败");
            }
        }
    }

    /**
     * 移动选项
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/moveOption")
    public Result moveOption(@RequestBody MoveOptionParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            return surveyService.moveOption(param);
        }
    }

    /**
     * 移动选项
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/moveQuestion")
    public Result moveQuestion(@RequestBody MoveQuestionParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            return surveyService.moveQuestion(param);
        }
    }

    /**
     * 获取单个题目及选项接口
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/getQuestionAndOption")
    public Result getQuestionAndOption(@RequestBody QuestionParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
        }
        QuestionAndOptionParam questionAndOptionParam = surveyService.getQuestionAndOption(param.getQuestionId());
        if(questionAndOptionParam!=null){
            return Result.success(questionAndOptionParam);
        }else{
            return Result.error("获取题目及选项成功失败", "获取题目及选项成功失败");
        }
    }

    /**
     * 获取所有的题目及选项接口
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/getAllQuestionAndOption")
    public Result getAllQuestionAndOption(@RequestBody SurveyFormParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
        }
        FormParam formParam = surveyService.getAllQuestionAndOptionByFormId(param.getFormId());
        if(formParam!=null){
            return Result.success(formParam);
        }else{
            return Result.error("获取表单所有题目及选项成功失败", "获取表单所有题目及选项成功失败");
        }
    }


    /**
     * 保存“表单设置”接口，支持创建/更新
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/saveFormConfig")
    @OperLog(operModule = NewsConstant.FORM_MODULE, operType = NewsConstant.SAVE_OPERATION, operDesc = "修改表单设置")
    public Result saveFormConfig(@RequestBody SurveyFormConfigParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            int result = surveyService.createOrUpdateSurveyFormConfig(param, user);
            if(result>0){
                return Result.success("添加/修改表单设置成功");
            }else{
                return Result.error("添加/修改表单设置失败", "添加/修改表单设置失败");
            }
        }
    }

    /**
     * 更新“表单-共享平台”接口
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/updateFormSharePlatform")
    public Result updateFormSharePlatform(@RequestBody SurveyFormShardPlatformListParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            int result = surveyService.updateSurveyFormSharePlatform(param);
            if(result>0){
                return Result.success("修改表单共享平台设置成功");
            }else{
                return Result.error("修改表单共享平台设置失败", "修改表单共享平台设置失败");
            }
        }
    }

    /**
     * 获取“表单设置”接口
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/getFormConfig")
    public Result getFormConfig(@RequestBody SurveyFormConfigParam param, @CurrentUser Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            if(StringUtils.isEmpty(param.getFormId())){
                return Result.error("formId参数不能为空", "formId参数不能为空");
            }
            SurveyFormConfig surveyFormConfig = surveyService.getSurveyFormConfigByFormId(param.getFormId());
            if(surveyFormConfig!=null){
                return Result.success(surveyFormConfig);
            }else{
                return Result.error("没有该智能表单对应的设置", "没有该智能表单对应的设置");
            }
        }
    }


    /**
     * 回收站恢复表单的接口
     * @param param
     * @param user
     * @return
     */
    @PostMapping("/restoreForm")
    @OperLog(operModule = NewsConstant.FORM_MODULE, operType = NewsConstant.RESTORE_OPERATION, operDesc = "恢复表单")
    public Result restoreForm(@RequestBody SurveyFormParam param, Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            return surveyService.restoreForm(param.getFormId());
        }
    }

    @PostMapping("/deleteFormFromRecycle")
    public Result deleteFormFromRecycle(@RequestBody SurveyFormParam param, Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            return surveyService.deleteFormFromRecycle(param.getFormId(), user);
        }
    }

    @PostMapping("/clearFormInRecycle")
    public Result clearFormInRecycle(Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            return surveyService.clearFormInRecycle(user);
        }
    }

    @PostMapping("/copyForm")
    public Result copyForm(@RequestBody CopyFormParam param, Users user){
        if(user==null){
            return Result.error(ResponseEnum.NO_TOKEN);
        }else{
            return surveyService.copyForm(param);
        }
    }

    @ApiOperation("获取表单统计数据(表格)")
    @RequestMapping("/loadSurveyFormStatistics")
    public Result loadSurveyFormStatistics(@RequestBody QueryFormParam param){
        return surveyService.loadSurveyFormStatistics(param);
    }

    @ApiOperation("获取表单统计数据(列表)")
    @RequestMapping("/loadSurveyFormStatisticsList")
    @OperLog(operModule = NewsConstant.FORM_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "表单数据导出")
    public Result loadSurveyFormStatisticsList(@RequestBody QueryFormParam param, @CurrentUser Users users){
        return surveyService.loadSurveyFormStatisticsList(param,users);
    }

    @ApiOperation("单个/批量修改选项提交次数")
    @RequestMapping("/updateSurveyFormOptionSubmitCount")
    @OperLog(operModule = NewsConstant.FORM_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "手动变更投票数量")
    public Result updateSurveyFormOptionSubmitCount(@RequestBody QueryFormParam param, @CurrentUser Users users){
        return surveyService.updateSurveyFormOptionSubmitCount(param,users);
    }
}
